package com.yuzhitong.shapi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.yuzhitong.shapi.BuildConfig;
import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.base.BaseLayoutActivity;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.util.AdUtilManage;
import com.yuzhitong.shapi.util.AppUtil;
import com.yuzhitong.shapi.util.LoggerUtil;

import a.a.a.MyApplication;

/**
 * Created by Brsya
 * CreateDate: 2022/4/26  16:55
 * Description:
 * 设置页面
 */
public class SettingActivity extends BaseLayoutActivity implements View.OnClickListener {

    private final String AD_KEY = "switch_ad_recommend";
    private final String CONTENT_KEY = "switch_content_recommend";

    private LinearLayout llTitleBackBox;
    private LinearLayout llContentUsBox;
    private LinearLayout llAboutBox;
    private LinearLayout llScoreBox;
    private LinearLayout llShearBox;
    private LinearLayout llAgreementUserBox;
    private LinearLayout llAgreementPrivateBox;
    private LinearLayout llHelpBox;
    private RelativeLayout llPageTitleBox;
    private RelativeLayout rlAdBox;

    // 广告管理类
    private AdUtilManage adUtilManage;
    private LinearLayout llComContentBox;
    private View vIconContent;
    private LinearLayout llComAdBox;
    private View vIconAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        setDarkStatus();
        initView();
        settingView();

        //QQ官方sdk授权
        //        Tencent.setIsPermissionGranted(true);
    }

    /**
     * 友盟分享
     * QQ与新浪QQ与新浪不需要添加Activity，但需要在使用QQ分享或者授权的Activity中，添加：
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private void initView() {
        llPageTitleBox = findViewById(R.id.ll_page_title_box);
        llTitleBackBox = findViewById(R.id.ll_title_back_box);
        llContentUsBox = findViewById(R.id.ll_content_us_box);
        llAboutBox = findViewById(R.id.ll_about_box);
        llScoreBox = findViewById(R.id.ll_score_box);
        llShearBox = findViewById(R.id.ll_shear_box);
        llAgreementUserBox = findViewById(R.id.ll_agreement_user_box);
        llAgreementPrivateBox = findViewById(R.id.ll_agreement_private_box);
        llHelpBox = findViewById(R.id.ll_help_box);
        rlAdBox = (RelativeLayout) findViewById(R.id.rl_ad_box);
        llComContentBox = findViewById(R.id.ll_com_content_box);
        vIconContent = findViewById(R.id.v_icon_content);
        llComAdBox = findViewById(R.id.ll_com_ad_box);
        vIconAd = findViewById(R.id.v_icon_ad);
    }

    private void settingView() {
        llPageTitleBox.setPadding(
                llPageTitleBox.getPaddingLeft(),
                llPageTitleBox.getPaddingTop() + getStatusBarHeight(),
                llPageTitleBox.getPaddingRight(),
                llPageTitleBox.getPaddingBottom()
        );

        llTitleBackBox.setOnClickListener(this);
        llContentUsBox.setOnClickListener(this);
        llAboutBox.setOnClickListener(this);
        llScoreBox.setOnClickListener(this);
        llShearBox.setOnClickListener(this);
        llAgreementUserBox.setOnClickListener(this);
        llAgreementPrivateBox.setOnClickListener(this);
        llHelpBox.setOnClickListener(this);
        llComContentBox.setOnClickListener(this);
        llComAdBox.setOnClickListener(this);

        adUtilManage = AdUtilManage.initShow(this, rlAdBox, Contents.AD_TT_SDK_SETTING_ID);


        if (!"0".equals(MyApplication.aCache.getAsString(AD_KEY))) {
            vIconAd.setBackgroundResource(R.mipmap.icon_switch_open);
        }else{
            vIconAd.setBackgroundResource(R.mipmap.icon_switch_close);
        }

        if (!"0".equals(MyApplication.aCache.getAsString(CONTENT_KEY))) {
            vIconContent.setBackgroundResource(R.mipmap.icon_switch_open);
        }else{
            vIconContent.setBackgroundResource(R.mipmap.icon_switch_close);
        }

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_title_back_box) {
            // 返回
            finish();
        } else if (id == R.id.ll_content_us_box) {
            // 联系我们
            startActivity(new Intent(this, AboutActivity.class));
        } else if (id == R.id.ll_about_box) {
            // 关于我们
            startActivity(new Intent(this, AboutActivity.class));
        } else if (id == R.id.ll_score_box) {
            // 给我们评分 跳转到应用商店app页面
            AppUtil.launchAppDetail(this, BuildConfig.APPLICATION_ID, "");
        } else if (id == R.id.ll_shear_box) {
            // 分享App 无效果
            shear();
        } else if (id == R.id.ll_agreement_user_box) {
            // 用户协议
            Intent intent = new Intent(SettingActivity.this, AgreementWebActivity.class);
            intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_USER);
            intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, getString(R.string.agreement_title_user));
            startActivity(intent);
        } else if (id == R.id.ll_agreement_private_box) {
            // 隐私协议
            Intent intent = new Intent(SettingActivity.this, AgreementWebActivity.class);
            intent.putExtra(Contents.INTENT_H5_AGREEMENT, Contents.AGREEMENT_URL_PRIVATE);
            intent.putExtra(Contents.INTENT_TITLE_AGREEMENT, getString(R.string.agreement_title_private));
            startActivity(intent);
        } else if (id == R.id.ll_help_box) {
            // 帮助反馈
            startActivity(new Intent(this, HelpActivity.class));
        } else if (id == R.id.ll_com_content_box) {
            // 个性化内容
            String openType;
            if ("0".equals(MyApplication.aCache.getAsString(CONTENT_KEY))) {
                vIconContent.setBackgroundResource(R.mipmap.icon_switch_open);
                openType = "1";
            }else{
                vIconContent.setBackgroundResource(R.mipmap.icon_switch_close);
                openType = "0";
            }
            MyApplication.aCache.put(CONTENT_KEY, openType);
        } else if (id == R.id.ll_com_ad_box) {
            // 个性化广告
            String openType;
            if ("0".equals(MyApplication.aCache.getAsString(AD_KEY))) {
                vIconAd.setBackgroundResource(R.mipmap.icon_switch_open);
                openType = "1";
            }else{
                openType = "0";
                vIconAd.setBackgroundResource(R.mipmap.icon_switch_close);
            }
            MyApplication.aCache.put(AD_KEY, openType);
        }
    }


    private void shear() {
        //        UMImage umImage = new UMImage(this, )
        UMWeb web = new UMWeb("http://download.yuzhitong.net/");
        web.setTitle(getResources().getString(R.string.app_name));//标题
        //        web.setThumb(thumb);//缩略图
        web.setDescription("");//描述
        new ShareAction(this)
                .withMedia(web)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        LoggerUtil.e("开始");
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        LoggerUtil.e("成功");
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
                        LoggerUtil.e("错误");
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        LoggerUtil.e("取消");
                    }
                })
                .open();
    }


    @Override
    protected void onDestroy() {
        AdUtilManage.destroy(adUtilManage);
        super.onDestroy();
    }
}
